package cn.seapon.talker.common;

import java.util.ArrayList;
import java.util.Arrays;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


/** 
 * 描述组Id与组名称的对应关系
 * @author 作者 E-mail: 
 * @version 创建时间：2015年1月2日 下午3:59:18 
 * 类说明 
 */
public class GroupInfo{
	
	//分组所有者
	private String account = "";
	private int count = 0;
	private ArrayList<GroupInfoEntity> groups = new ArrayList<GroupInfo.GroupInfoEntity>();
	
	public void add(GroupInfoEntity entity){
		groups.add(entity);
		count++;
	}
	
	public GroupInfoEntity[] getGroupArray(){
		return groups.toArray(new GroupInfoEntity[count]);
	}
	
	public JSONArray getJsonArray(){
		JSONArray jsonArray = new JSONArray();
		try {
			for (int i = 0; i < groups.size(); i++) {
					GroupInfoEntity entity = groups.get(i);
					JSONObject jsonObject = new JSONObject();
					
					String ownerId = entity.getOwnerId();
					int groupId = entity.getGroupId();
					String groupNameString = entity.getGroupNameString();
					int groupIndex = entity.getGroupIndex();
					
					jsonObject.put("ownerId", ownerId);
					jsonObject.put("groupId", groupId);
					jsonObject.put("groupNameString", groupNameString);
					jsonObject.put("groupIndex", groupIndex);
					
					jsonArray.put(jsonObject);
				}
			} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return jsonArray;
	}
	
	public static GroupInfo getObjectFromJson(JSONArray jsonArray){
		GroupInfo infos = new GroupInfo();
		try {
			for (int i = 0; i < jsonArray.length(); i++) {
				JSONObject object = (JSONObject) jsonArray.get(i);
				GroupInfoEntity entity = new GroupInfoEntity();
				
				String ownerId = object.getString("ownerId");
				int groupId = object.getInt("groupId");
				String groupNameString = object.getString("groupNameString");
				int groupIndex = object.getInt("groupIndex");
				
				entity.setGroupId(groupId);
				entity.setGroupIndex(groupIndex);
				entity.setGroupNameString(groupNameString);
				entity.setOwnerId(ownerId);
				infos.add(entity);
			}
		} catch (JSONException e) {
			e.printStackTrace();
			return null;
		}
		return infos;
	}
	
	
	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public ArrayList<GroupInfoEntity> getGroups() {
		return groups;
	}

	public void setGroups(ArrayList<GroupInfoEntity> groups) {
		this.groups = groups;
	}

	public GroupInfo() {
	}

	public GroupInfo(String account,ArrayList<GroupInfoEntity> groups) {
		super();
		this.account = account;
		this.groups = groups;
	}
	
	/**
	 * 通过组ID获取名称
	 * @param groupId
	 * @return
	 */
	public String getGroupName(int groupId){
		for (int i = 0; i < groups.size(); i++) {
			if (groups.get(i).getGroupId() == groupId) {
				return groups.get(i).getGroupNameString();
			}
		}
		return null;
	}
	
	public int getGroupId(String name){
		for (int i = 0; i < groups.size(); i++) {
			if (groups.get(i).getGroupNameString() == name) {
				return groups.get(i).getGroupId();
			}
		}
		return -1;
	}
	
	/**
	 * 通过组ID获取名称
	 * @param groupId
	 * @return
	 */
	public int getGroupSize(int groupId){
		int count = 0;
		for (int i = 0; i < groups.size(); i++) {
			if (groups.get(i).getGroupId() == groupId) {
				count++;
			}
		}
		return count;
	}
	
	/**
	 * 获取组排列顺序
	 * @author Asuron
	 * Created on 2015年1月2日 下午7:24:29
	 */
	public int[] getGroupIdByOrder(){
		int[] arr = new int[groups.size()];
		int count = 0;
		int lastGroupIdgroupId = -1;
		for (int i = 0; i < groups.size(); i++) {
			int groupId = groups.get(i).getGroupId();
			if (groupId != lastGroupIdgroupId) {
				arr[count++] = groupId;
				lastGroupIdgroupId = groupId;
			}
		}
		//去除空的单元
		int[] newArr = Arrays.copyOf(arr, count) ;
		Arrays.sort(newArr);
		return newArr;
	}


	public static class GroupInfoEntity {
		//请求数据 
		private String ownerId;
		
		private int groupId;
		private String groupNameString;
		private int groupIndex;//顺序 越小越靠前
		
		public GroupInfoEntity(String ownerId, int groupId,
				String groupNameString, int groupIndex) {
			this.ownerId = ownerId;
			this.groupId = groupId;
			this.groupNameString = groupNameString;
			this.groupIndex = groupIndex;
		}
		public GroupInfoEntity() {
		}
		public String getOwnerId() {
			return ownerId;
		}
		public void setOwnerId(String ownerId) {
			this.ownerId = ownerId;
		}
		public int getGroupId() {
			return groupId;
		}
		public void setGroupId(int groupId) {
			this.groupId = groupId;
		}
		public String getGroupNameString() {
			return groupNameString;
		}
		public void setGroupNameString(String groupNameString) {
			this.groupNameString = groupNameString;
		}
		public int getGroupIndex() {
			return groupIndex;
		}
		public void setGroupIndex(int groupIndex) {
			this.groupIndex = groupIndex;
		}
		
	
	}

}
